/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Seoprofesional
 *
 * The Initial Developer of the Original Code is
 * Petr Blahoš.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

SeoprofesionalTools = {
cleanHttpsURL: function(url) {
   var match = url.match(/^https:\/\/[^/]+\//);
   return match ? match[0] : url;
},
format_date: function(d, fmt, monthNames) {
    var leftPad = function(n) {
  n = "" + n;
  return n.length == 1 ? "0" + n : n;
    };
    var r = [];
    var escape = false;
    if (monthNames == null)
      monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    for (var i = 0; i < fmt.length; ++i) {
      var c = fmt.charAt(i);
      if (escape) {
          switch (c) {
          case 'h': c = "" + d.getUTCHours(); break;
          case 'H': c = leftPad(d.getUTCHours()); break;
          case 'M': c = leftPad(d.getUTCMinutes()); break;
          case 'S': c = leftPad(d.getUTCSeconds()); break;
          case 'd': c = "" + d.getUTCDate(); break;
          case 'm': c = "" + (d.getUTCMonth() + 1); break;
          case 'y': c = "" + d.getUTCFullYear(); break;
          case 'b': c = "" + monthNames[d.getUTCMonth()]; break;
          }
          r.push(c);
          escape = false;
      } else {
        if (c == "%") {
          escape = true;
        } else {
          r.push(c);
        }
      }
    }
    return r.join("");
},
getHttpDomain: function(url) {
    // returns https?://domain.xx/
    var match = url.match(/^https?:\/\/[^/]+\//);
    return match ? match[0] : url;
},
get_domain: function(uri) {
  var ss = uri.indexOf("//");
  if (-1!=ss) {
    uri = uri.substr(ss+2);
  } // else: wierd, looks like the uri doesn't start with something://
  ss = uri.indexOf("/");
  if (-1!=ss) {
    uri = uri.substr(0, ss);
  }
  return uri;
},

getRequestParams: function(uri, prefix) {
  if (!prefix) {
    prefix = "";
  }
  prefix+= "?";
  var start = uri.indexOf(prefix);
  if (-1==start) {
    return {};
  }
  start+= prefix.length;
  var spl = uri.substr(start).split("&");
  var ret = {};
  for (var i = 0;i<spl.length;i++) {
    var part = spl[i];
    start = part.indexOf("=");
    if (-1==start) {
      ret[part] = "";
    } else {
      ret[part.substr(0, start)] = part.substr(start+1);
    }
  }
  return ret;
},
translate: function() {
  var els = document.querySelectorAll(".trans");
  for (var i = 0;i<els.length;i++) {
    els[i].innerText = chrome.i18n.getMessage(els[i].innerText);
  }
},
/**
 * If the number is > 9999 displays according to the following schema
 * so that it fits in 4 characters:
 * 10K
 * 123K
 * 0.8M
 * 1.3M
 * 13M
 * 130M
 */
suffixes: [ "k", "M", "G", "T", "P", "E", "Z", "Y" ],
suffixize: function(n) {
  n = Math.round(n);  // should not be "float" but just for case
  if (n<10000)  return ""+n;
  var prefix;
  for (var i = 0;i<SeoprofesionalTools.suffixes.length;i++) {
    prefix = SeoprofesionalTools.suffixes[i];
    n = n/1000;
    if (n<1000) {
      break;
    }
  }
  n = ""+n;
  if (n.length>3) n = n.substr(0, 3);
  if ("."==n[n.length-1]) n = n.substr(0, n.length-2);
  return n+prefix;
},
default_menulinks: [
          [true, chrome.i18n.getMessage("whois"),               "http://network-tools.com/default.asp?prog=whois&host=%d", true ],
          [true, chrome.i18n.getMessage("majesticseo"),         "http://www.majesticseo.com/search.php?folder=&q=%s", true ],
          [true, chrome.i18n.getMessage("linkscape"),           "http://www.seomoz.org/linkscape/intel/basic/?uri=%s", true ],
          [true, chrome.i18n.getMessage("backlinks_yahoo"),     "http://siteexplorer.search.yahoo.com/search?bwm=i&bwmf=u&bwms=p&fr=siteexplorer&fr2=seo-rd-se&p=%s", true ],
          [true, chrome.i18n.getMessage("site_count_seznam"),   "http://search.seznam.cz/?q=site:%d&mod=f", true ],
          [true, chrome.i18n.getMessage("site_count_google"),   "http://www.google.com/search?q=site:%d", true ],
          [true, chrome.i18n.getMessage("google_analytics"),    "https://www.google.com/analytics", true ],
          [true, chrome.i18n.getMessage("google_kw_tool"),      "https://adwords.google.com/select/KeywordToolExternal", true ],
          [true, chrome.i18n.getMessage("google_insights"),     "http://www.google.com/insights/search/", true ],
          [true, chrome.i18n.getMessage("seznam_searchability"),"http://search.seznam.cz/stats?collocation=seo", true ]
        ],

_x_sentinel: 0
}

